<?php

require "./includes/class.template.php";
require "site_whois.php";

$template = new verySimpleTemplate();
$domain = @$_GET['domain'];

if( !strpos( $domain, '.' ) )
{
	$domain .= '.com';
}

switch( @$_GET['action'] )
{
	case 'pagerank':
	echo pageRank( $domain );
	break;

	case 'google':
	echo google( $domain );
	break;

	case 'baidu':
	echo baidu( $domain );
	break;

	case 'msn':
	echo msn( $domain );
	break;

	case 'yahoo':
	echo yahoo( $domain );
	break;

	case 'yisou':
	echo yisou( $domain );
	break;

	case 'so163':
	echo so163( $domain );
	break;

	case 'sina':
	echo sina( $domain );
	break;

	case 'qq':
	echo qq( $domain );
	break;

	case 'sohu':
	echo sohu( $domain );
	break;

	case 'whois':
	echo whois( $domain );
	break;

	case 'alexa':
	echo alexa( $domain );
	break;

	default:
	break;
}

function getContent( $url, $start, $end )
{
	$content = implode( '', file( $url ) );
    $content = substr( strstr( $content, $start ), strlen($start) );
    $content = substr_replace( $content, '', strpos( $content, $end ) );
	return $content;
}

function pageRank( $domain )
{
	global $template;

	$urlF  = 'http://rankwhere.com/google-page-rank.php?url=' . $domain;
	$urlS  = 'http://rankwhere.com/google-page-rank.php?url=www.' . $domain;
	$rankF = getContent( $urlF, 'has Google PageRank ' , ' out of 10' );
	$rankS = getContent( $urlS, 'has Google PageRank ' , ' out of 10' );
	$rankF  = intval( $rankF );
	$rankS  = intval( $rankS );
	$body  = '<div align=left>www.' . $domain . '在满分10分评价中获得 <b>' . $rankS . '</b> 分&nbsp;&nbsp;|&nbsp;&nbsp;' . $domain . '在满分10分评价中获得 <b>' . $rankF . '</b> 分</div>';;

	$arrayVar = array ( 'CHECK_CAT'  => 'dopagerank',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function google( $domain )
{
	global $template;

	$url       = 'http://www.google.com/search?hl=zh-CN&q=site%3A' . $domain;
	$pageCount = getContent( $url, '</b> 上约有 <b>', '</b> 项符合<b></b>的查询' );
	if( $pageCount == '' )
	{
		$pageCount = getContent( $url, '</b> 上共有 <b>', '</b> 项符合<b></b>的查询' );
	}
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain . '在google上的搜索结果：找到相关网页约<b> ' . $pageCount . " </b>篇.&nbsp;&nbsp;<a href='http://www.google.com/search?hl=zh-CN&q=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'dogoogle',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function baidu( $domain )
{
	global $template;

	$url       = 'http://www.baidu.com/baidu?word=site%3A' . $domain;
	$pageCount = getContent( $url, '<nobr>找到相关网页', '篇，用时' );
	$pageCount = str_replace( '约', '', $pageCount );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在baidu上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://www.baidu.com/baidu?word=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'dobaidu',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function msn( $domain )
{
	global $template;

	$url       = 'http://search.msn.com/results.aspx?FORM=PERE&q=site%3A' . $domain;
	$pageCount = getContent( $url, ' of ', ' containing <strong>' );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在msn上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://search.msn.com/results.aspx?FORM=PERE&q=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'domsn',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function yahoo( $domain )
{
	global $template;

	$url       = 'http://cn.search.yahoo.com/search?ei=UTF-8&p=site%3A' . $domain;
	$pageCount = getContent( $url, '网页结果</h1><p>约<strong>', '</strong>项 第' );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在yahoo上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://cn.search.yahoo.com/search?ei=UTF-8&p=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'doyahoo',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function yisou( $domain )
{
	global $template;

	$url       = 'http://www.yisou.com/search?p=site%3A' . $domain;
	$pageCount = getContent( $url, " &nbsp;&nbsp;约&nbsp;<b>", '</b>&nbsp;项&nbsp;第' );
	$pageCount = str_replace( '\r', '', $pageCount );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在yisou上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://www.yisou.com/search?p=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'doyisou',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function sina( $domain )
{
	global $template;

	$url       = 'http://search.sina.com.cn/cgi-bin/search/search.cgi?_searchkey=site%3A' . $domain;
	$startPos  = $domain . '</font> 网站结果 ';
	$pageCount = getContent( $url, $startPos, ' 个，<font class="t1">' );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在sina上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://search.sina.com.cn/cgi-bin/search/search.cgi?_searchkey=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  =>'dosina',
						'RESULT_BODY'=>$body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function so163( $domain )
{
	global $template;

	$url       = 'http://page.so.163.com/psearch.php?q=site%3A' . $domain;
	$pageCount = getContent( $url, '共</span><SPAN class=black5> ', " </span><span class='black4 style4 style10'>页" );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在163上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://page.so.163.com/psearch.php?q=site%3A" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'doso163',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function qq( $domain )
{
	global $template;

	$url       = 'http://so.qq.com/cgi-bin/search?word=' . $domain;
	$pageCount = getContent( $url, '</font> </strong>相关网页', '篇.用时[' );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在QQ上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://so.qq.com/cgi-bin/search?word=" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'doqq',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function sohu( $domain )
{
	global $template;

	$url       = 'http://search.sohu.com/websearch/websearch.do?query=' . $domain;
	$pageCount = getContent( $url, '<small>共找到 ', ' 个结果，总用时' );
	$pageCount = intval( str_replace( ',', '', $pageCount ) );
	$body      = '<div align=left>' . $domain .  '在sohu上的搜索结果：找到相关网页约<b> ' . $pageCount .  "</b>篇.&nbsp;&nbsp;<a href='http://search.sohu.com/websearch/websearch.do?query=" . $domain . "' target=_blank><font color=red><u>点击察看详细内容</u></font></a></div>";

	$arrayVar = array ( 'CHECK_CAT'  => 'dosohu',
						'RESULT_BODY'=> $body
					  );
	$template->deal( 'template/site_frameBody.tpl', $arrayVar );

	return $template->template;
}

function whois( $domain )
{
	global $template;
	
	$allowed = '';
	$returnStr = '';
	$i = cWhois( $domain, $domext = '', $Reg = '');
	if( ( $i == 0 ) || ( $i == 1 ) )
	{
		foreach ( $Reg as $line )
		{
			$returnStr .= $line . '<br>';
		}
	}
	else
	{
		$returnStr = 'Could not get the whois info...';
	}

	$arrayVar = array ( 'WHOIS_INFO'  => $returnStr );
	$template->deal( 'template/site_whoisBody.tpl', $arrayVar );

	return $template->template;
}

function alexa( $domain )
{
	global $template;
	$judge = $judgePieces = array();

	$content = implode( '', file( 'http://www.alexa.com/data/details/traffic_details?q=&url=' . $domain ) );

	$judgePieces = explode( ':</span>&nbsp;', $content );
	$judge = @explode( '<div class', $judgePieces[1] );
	if( $judge[0] != 'No Data' )
	{
		$content = str_replace( '>--<', '-->--</td>', $content );
		$pieces = explode( '-->', $content );
		for( $i = 2; $i<=5; $i++ )
		{
			$smallPieces[$i] = explode( '</td>', $pieces[$i] );
		}
		$changePieces = explode( '>', $smallPieces[4][1] );
		$tranficRankPieces = explode( '<div', $pieces[1] );

		$today       = $smallPieces[2][0];
		$week        = $smallPieces[3][0];
		$month       = $smallPieces[4][0];
		if( $smallPieces[5][0] != '--' )
		{
			$change      = $changePieces[1] . '>&nbsp;' . $smallPieces[5][0];
		}
		else
		{
			$change = '--';
		}
		$tranficRank = $tranficRankPieces[0];
	}
	else
	{
		$today		 = '0';
		$week		 = '0';
		$month		 = '0';
		$change		 = '0';
		$tranficRank = '0';
	}
	$dailyReach  = 'http://traffic.alexa.com/graph?w=379&h=216&r=6m&y=r&u=' . $domain . '&u=';
	$detail      = 'http://www.alexa.com/data/details/traffic_details?q=&url=' . $domain;

	$arrayVar = array ( 'TRANFIC_RANK'  => $tranficRank,
						'TODAY'         => $today,
						'MONTH'			=> $month,
						'WEEK'			=> $week,
						'DETAIL'		=> $detail,
						'DAILY_REACH'	=> $dailyReach,
						'CHANGE'		=> $change,
						'DOMAIN'		=> $domain
					  );
	$template->deal( 'template/site_alexaBody.tpl', $arrayVar );

	return $template->template;
}


?>